//
//  LGBreathRecordDetailController.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/8/6.
//

#import "LGBreathRecordDetailController.h"
//总体情况
#import "LGBreathReportSituationView.h"
//过程趋势图
#import "LGBreathReportChartView.h"
//测量值
#import "LGBreathReportValueView.h"
@interface LGBreathRecordDetailController ()
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**  总体情况 */
@property (nonatomic , strong)LGBreathReportSituationView *situationView;
/**  过程趋势图 */
@property (nonatomic , strong)LGBreathReportChartView *chartView;
/**  测量值 */
@property (nonatomic , strong)LGBreathReportValueView *valueView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , copy)NSString *type;

@end

@implementation LGBreathRecordDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/pranayamaReport" parameters:@{@"patient_id":self.patientid,@"record_id":LGNSString(self.dataDic[@"record_id"]),@"finish_time":LGNSString(self.dataDic[@"finish_time"])} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            //总体情况
            NSArray *tempArray1 = returnData[@"time_table"];
            for (NSInteger i=0; i<tempArray1.count; i++) {
                NSDictionary *tempDic = tempArray1[i];
                if (i == 0) {
                    self.situationView.planLabel1.text = LGNSString(tempDic[@"plan"]);
                    self.situationView.resultLabel1.text = LGNSString(tempDic[@"record"]);
                }else{
                    self.situationView.planLabel2.text = LGNSString(tempDic[@"plan"]);
                    self.situationView.resultLabel2.text = LGNSString(tempDic[@"record"]);
                }
            }
            //测量值
            NSArray *tempArray2 = returnData[@"measure_table"];
            for (NSInteger i=0; i<tempArray2.count; i++) {
                NSDictionary *tempDic = tempArray2[i];
                if (i == 0) {
                    self.valueView.averageLabel1.text = LGNSString(tempDic[@"avg"]);
                    self.valueView.optimalLabel1.text = LGNSString(tempDic[@"max"]);
                }else if(i == 1){
                    self.valueView.averageLabel2.text = LGNSString(tempDic[@"avg"]);
                    self.valueView.optimalLabel2.text = LGNSString(tempDic[@"max"]);
                }else{
                    self.valueView.averageLabel3.text = LGNSString(tempDic[@"avg"]);
                    self.valueView.optimalLabel3.text = LGNSString(tempDic[@"max"]);
                }
            }
            
            //过程趋势图
            self.chartView.dataArray = returnData[@"xAxis"];
        }
    } error:^(NSError *error) {
        
    }];
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    //1-排痰 、 2-呼吸
    self.type = LGNSString(dataDic[@"type"]);
    self.title = LGNSString(dataDic[@"title"]);
    NSString *title = [NSString stringWithFormat:@"%@ %@训练报告",self.recordDate,dataDic[@"title"]];
    self.titleLabel.attributedText = [title attributeString:@{NSFontAttributeName:LGFontWeight(20, UIFontWeightMedium),NSForegroundColorAttributeName:[UIColor colorWithString:@"#0A4AD4"]} range:NSMakeRange(0, self.recordDate.length)];
    
}




#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(364))];
    bgImageView.image = [UIImage imageNamed:@"呼吸训练-报告背景"];
    [self.view addSubview:bgImageView];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];
    [scrollView addSubview:self.titleLabel];
    [scrollView addSubview:self.situationView];
    [scrollView addSubview:self.chartView];
    [scrollView addSubview:self.valueView];
    scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.valueView.frame)+viewPix(50));
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(10), viewPix(15), Screen_W-viewPix(20), viewPix(26));
    }
    return _titleLabel;
}

-(LGBreathReportSituationView *)situationView{
    if(!_situationView){
        _situationView = [[LGBreathReportSituationView alloc]initWithTitle:@"总体情况" frame:CGRectMake(viewPix(16), viewPix(52), Screen_W-viewPix(32), viewPix(206))];
    }
    return _situationView;
}
//1-排痰 、 2-呼吸
-(LGBreathReportChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGBreathReportChartView alloc]initWithTitle:@"过程趋势图" frame:CGRectMake(viewPix(16), kMaxY(self.situationView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(215))];
        _chartView.typeTitle = [self.type isEqualToString:@"1"]?@"每次振幅(cmH2O)":@"每次吸气功率(W)";
    }
    return _chartView;
}
//1-排痰 、 2-呼吸
-(LGBreathReportValueView *)valueView{
    if(!_valueView){
        NSArray *titleArray = [self.type isEqualToString:@"1"]?@[@"指标",@"压力(cmH2O)",@"振幅(cmH2O)",@"呼吸时长(s)"]:@[@"指标",@"压力(cmH2O)",@"功率(W)",@"能量(J)"];
        _valueView = [[LGBreathReportValueView alloc]initWithTitle:@"测量值" titleArray:titleArray  frame:CGRectMake(viewPix(16), kMaxY(self.chartView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(225))];
    }
    return _valueView;
}

@end
